#!/usr/bin/wish8.5


#This is an extremely simple gui frontend for FCEU for Linux
wm title . "Paul's MEDNAFEN frontend"

#Pulldown menu
frame .mbar -relief raised -bd 2
pack .mbar -side top -fill x
menubutton .mbar.file -text File -underline 0 -menu .mbar.file.menu
menubutton .mbar.options -text Options -underline 0 -menu .mbar.options.menu
menu .mbar.options.menu
menu .mbar.file.menu
##Defalut screen size is 3
set screensize 4;
.mbar.options.menu add radiobutton -label 1x -variable screensize -value 1
.mbar.options.menu add radiobutton -label 2x -variable screensize -value 2
.mbar.options.menu add radiobutton -label 3x -variable screensize -value 3
.mbar.options.menu add radiobutton -label 4x -variable screensize -value 4
.mbar.options.menu add separator
.mbar.options.menu add checkbutton -label "Set Controls" -variable set_ctl
.mbar.file.menu add command -label Quit -underline 0 -command {exit}
pack .mbar.file -side left
pack .mbar.options -side left
tk_menuBar .mbar .mbar.options
focus .mbar



#Widgets for setting rom path
label .romlabel -text "Rom Path"
label .romsearch -text "Optional regex keywords:"
set rompath "/home/paul/miscprograms/NESroms"
entry .rom -width 100 -relief sunken -textvariable rompath
entry .search -width 30 -relief sunken -textvariable keywords
button .updatelist -text "Update File List" -command {getList $rompath $keywords}
bind .rom <Return> {getList $rompath $keywords}
bind .search <Return> {getList $rompath $keywords}
pack .romlabel .rom .romsearch .search .updatelist -side top -padx 2m -expand 1


#Widgets for globbing all files in $rompath
listbox .romlist -relief raised -yscrollcommand ".scroll set"
bind .romlist <Double-Button-1> {launchRom $screensize $set_ctl; set set_ctl 0}
scrollbar .scroll -command ".romlist yview"
pack .scroll -side right -fill y
pack .romlist -fill both

#Widgets for Launching/Monitoring FCEU:
#Create new frame for FCEU-related widgets
frame .fceuframe -relief sunken -width 3c -height 2c
text .fceuframe.stdo -relief sunken 
label .fceuframe.fceulabel -text "FCEU Standard Output:"
button .fceuframe.launch -text "Start Emulation" -command {launchRom $screensize $set_ctl; set set_ctl 0 }
pack .fceuframe.launch -side top
pack .fceuframe -side bottom -padx 2m
#pack .fceuframe.fceulabel -side bottom


#User-defined functions:
proc getList {rompath keywords} {
    .romlist delete 0 end
    cd $rompath
    foreach file [lsort [glob *]] {
	for {set i 0} {$i < [llength $keywords]} {incr i} {
	    set keyword [lindex $keywords $i]

	    #if file matches keyword and keyword is last, insert file
	    if {[regexp -nocase $keyword $file] && $i == [expr [llength $keywords] - 1]} {
		.romlist insert end $file
	    } elseif {[regexp -nocase $keyword $file]} {
		continue
	    } else {
		break
	    }
	}
    }
}

proc launchRom {screensize set_controls} {
    if {$set_controls} {
	set controls "-inputcfg gamepad1"
    } else {
	set controls ""
    }

    set cmd_string "mednafen \"[selection get]\""
    eval exec $cmd_string
}
